package com.xlh.virtualization.controller;

import com.xlh.common.Result;
import com.xlh.common.ResultEnum;
import com.xlh.param.courseTest.ExperimentRunJudgeParam;
import com.xlh.pojo.courseTest.ExperimentStatisticsTaskPass;
import com.xlh.service.courseTest.ExperimentStatisticsStepPassService;
import com.xlh.service.courseTest.ExperimentStatisticsTaskPassService;
import com.xlh.util.BeanUtil;
import com.xlh.virtualization.exception.CheckException;
import com.xlh.virtualization.po.JudgeSavePO;
import com.xlh.virtualization.service.ExperimentJudgeService;
import com.xlh.virtualization.vo.JudgeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cheer
 */
@Api("评测操作")
@RestController
@RequestMapping("/experiment/judge")
public class ExperimentJudgeController {

    @Autowired
    private ExperimentJudgeService experimentJudgeService;

    @Autowired
    private ExperimentStatisticsStepPassService experimentStatisticsStepPassService;


    @ApiOperation("添加评测")
    @PostMapping("/save")
    public Result saveJudge(@RequestBody @Valid JudgeSavePO judgeSavePO) {
        experimentJudgeService.saveJudge(judgeSavePO);
        return Result.success();
    }

    @ApiOperation("查询评测")
    @GetMapping("/info")
    public Result getJudge(@RequestParam Integer type, @RequestParam Long contextId) {
        JudgeVO judgeVO = experimentJudgeService.getJudge(type, contextId);
        return Result.success(judgeVO);
    }

    @ApiOperation("查询是否有评测")
    @GetMapping("/exists")
    public Result existsJudge(@RequestParam Integer type, @RequestParam Long contextId) {
        return Result.success(experimentJudgeService.existsJudge(type, contextId));
    }

    @ApiOperation("进行评测")
    @GetMapping("/run")
    public Result getJudge(@Valid ExperimentRunJudgeParam experimentRunJudgeParam) {
        try {
            Integer type = experimentRunJudgeParam.getType();
            Long contextId = experimentRunJudgeParam.getContextId();
            Long containerId = experimentRunJudgeParam.getContainerId();

            experimentJudgeService.runJudge(type, contextId, containerId);
            experimentStatisticsStepPassService.saveStepPass(experimentRunJudgeParam);
        } catch (CheckException e) {
            return Result.error(ResultEnum.CHECK_ERROR, e.getMessage());
        }
        return Result.success();
    }
}
